<?php
App::uses('AppController', 'Controller');
/**
 * Templates Controller
 *
 * @property Template $Template
 * @property PaginatorComponent $Paginator
 */
class TemplatesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
	public $theme = 'BackEnd';
	public $layout = 'admin';

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Template->recursive = 0;
		$this->set('templates', $this->Paginator->paginate());
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
		
		if( isset($_POST['action']) && $_POST['action'] == 'new_template' ){
		
			$name = $_POST['name'];
			$categories = $_POST['categories'];
			
			if( $_POST["id_copy"] !== ""){
				$template_copy = $this->Template->findById($_POST["id_copy"]);
				$content = $template_copy['Template']['content'];
			} else {
				$content = "";
			}
			
			$data = array(
				'Template' => array(
					'name' => $name,
					'content' => $content,
				)
			);
			
			$this->Template->create();
			
			if ($this->Template->save($data)) {
			
				$template_id = $this->Template->id;
				
				foreach($categories as $category){
					$data = array(
						'CategoriesTemplate' => array(
							'template_id' => $template_id,
							'category_id' => $category
						)
					);
					$this->Template->CategoriesTemplate->create();
					$this->Template->CategoriesTemplate->save($data);
				}
				echo $template_id;	
			}

			die();
		}
	}
	
	public function admin_app($id=null){
		$template_record = $this->Template->findById($id);
		if(!$template_record){
			$this->redirect('index');
		};
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if( isset($_POST['action']) && $_POST['action'] == 'saved' ){
			$content = $_POST['data'];
			$data = array(
				'Template' => array(
					'content' => $content,
					'id' => $id
				)
			);
			if($this->Template->save($data)){
				
				$img = $_POST['base64data'];
				$img = str_replace('data:image/png;base64,', '', $img);
				$img = str_replace(' ', '+', $img);
				$data = base64_decode($img);
				
				$file_path = APP . 'View/Themed/BackEnd/webroot/img/templates/' . $id .'.png';
				
				$file = new File($file_path);
				
				if($file->exists()){
					$file->delete();
				}
				
				file_put_contents($file_path, $data);
				
				echo "saved";
			}
			die();
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Template->id = $id;
		if (!$this->Template->exists()) {
			throw new NotFoundException(__('Invalid template'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Template->delete()) {
			$this->Session->setFlash(__('The template has been deleted.'));
		} else {
			$this->Session->setFlash(__('The template could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow();
	}
	
	public function admin_modal(){
		if($_POST['action'] && $_POST['action'] == 'modal'){
			$categories = $this->Template->Category->generateTreeList();
			echo(json_encode($categories));
			die();
		}
	}
	
	public function admin_get(){
		if($_POST['action'] && $_POST['action'] == 'get'){
			$id = $_POST['id'];
			$template = $this->Template->findById($id);
			if($template){
				echo $template['Template']['content'];
			} else {
				echo "0";
			}
			die();
		}
	}
}
